import {Store} from 'iflux2'
import {Map, List, fromJS} from 'immutable'
import MenuActor from './actor/menu-actor'
import * as types from './constants/actionTypes'
import WebApi from './webapi'
export default class AppStore extends Store {

    bindActor() {
        return [
            new MenuActor
        ]
    }

    toggleMenu = async (status) => {
        await WebApi.toggleMenu();
        this.dispatch(types.TOGGLE_MENU, status);
    };

    fetchMenu = async () => {
        const {menu} = await WebApi.fetchMenu();
        // console.log(menu);
        this.dispatch(types.FETCH_MENU, fromJS(menu.button));
        return fromJS(menu.button);
    };

    saveMenu = async () => {
        const button = this.state().get("button");
        const result = await WebApi.createMenu(button.toJS());
        if (result.errcode === 0) {
            this.dispatch(types.UPDATE_MENU, fromJS(button));
        }
        return result;
    };

    deleteMenu = async () => {
        const result = await WebApi.deleteMenu();
        this.dispatch(types.SELECT_ITEM, -1);
        this.dispatch(types.SELECT_SUB_ITEM, -1);
        this.dispatch(types.DELETE_MENU);
        return result;
    };

    selectItem = (index) => {
        const selectedSubIndex = this.state().get("selectedSubIndex");
        const selectedIndex = this.state().get("selectedIndex");
        if (selectedIndex === index && selectedSubIndex === -1) return;
        this.dispatch(types.SELECT_ITEM, index);
        this.dispatch(types.SELECT_SUB_ITEM, -1);
    };

    selectSubItem = (index) => {
        const selectedSubIndex = this.state().get("selectedSubIndex");
        if (selectedSubIndex === index) return;
        this.dispatch(types.SELECT_SUB_ITEM, index);
    };


    addItem = () => {
        // console.log(this.state());
        const button = this.state().get("button");
        const selectedIndex = this.state().get("selectedIndex");
        const item = {
            name: "菜单标题",
            type: "click",
            key: "text__",
            sub_button: []
        };
        this.dispatch(types.UPDATE_MENU, button.push(fromJS(item)));
        this.dispatch(types.SELECT_ITEM, button.size);
        this.dispatch(types.SELECT_SUB_ITEM, -1);
    };

    addSubItem = () => {
        let button = this.state().get("button");
        const selectedIndex = this.state().get("selectedIndex");
        const subItem = {
            name: "子菜单标题",
            type: "click",
            key: "text__",
        };
        button = button.updateIn(
            [selectedIndex, "sub_button"], arr => arr.push(fromJS(subItem))
        );
        this.dispatch(types.UPDATE_MENU, button);
        this.dispatch(types.SELECT_SUB_ITEM, button.getIn([selectedIndex, "sub_button"]).size - 1)
    };


    moveItem = (dragIndex, hoverIndex) => {
        let button = this.state().get("button");
        const selectedIndex = this.state().get("selectedIndex");
        const dragItem = button.get(dragIndex);
        const targetItem = button.get(hoverIndex);
        button = button.set(dragIndex, targetItem).set(hoverIndex, dragItem);
        this.dispatch(types.UPDATE_MENU, button);
        if (selectedIndex === dragIndex) {
            this.dispatch(types.SELECT_ITEM, hoverIndex);
        }
    };

    moveSubItem = (dragIndex, hoverIndex) => {
        // console.log(dragIndex, hoverIndex);
        const selectedIndex = this.state().get("selectedIndex");
        const selectedSubIndex = this.state().get("selectedSubIndex");
        let button = this.state().get("button");
        const dragIndexPath = [selectedIndex, "sub_button", dragIndex];
        const hoverIndexPath = [selectedIndex, "sub_button", hoverIndex];
        const dragItem = button.getIn(dragIndexPath);
        const targetItem = button.getIn(hoverIndexPath);
        button = button.setIn(dragIndexPath, targetItem).setIn(hoverIndexPath, dragItem);
        this.dispatch(types.UPDATE_MENU, button);
        if (selectedSubIndex === dragIndex) {
            this.dispatch(types.SELECT_SUB_ITEM, hoverIndex);
        }
    };

    updateItem = (data) => {
        const selectedSubIndex = this.state().get("selectedSubIndex");
        const selectedIndex = this.state().get("selectedIndex");
        let button = this.state().get("button");
        button = button.updateIn(
            [selectedIndex], item => fromJS({...item.toJS(), ...data})
        );
        this.dispatch(types.UPDATE_MENU, button);
    };

    updateSubItem = (data) => {
        const selectedSubIndex = this.state().get("selectedSubIndex");
        const selectedIndex = this.state().get("selectedIndex");
        let button = this.state().get("button");
        button = button.updateIn(
            [selectedIndex, "sub_button", selectedSubIndex],
            item => fromJS({...item.toJS(), ...data})
        );
        this.dispatch(types.UPDATE_MENU, button);
    };

    deleteItem = () => {
        const selectedSubIndex = this.state().get("selectedSubIndex");
        const selectedIndex = this.state().get("selectedIndex");
        let button = this.state().get("button");
        button = button.deleteIn([selectedIndex]);
        this.dispatch(types.UPDATE_MENU, button);
        this.dispatch(types.SELECT_ITEM, button.size > 0 ? 0 : -1);
    };

    deleteSubItem = () => {
        const selectedSubIndex = this.state().get("selectedSubIndex");
        const selectedIndex = this.state().get("selectedIndex");
        let button = this.state().get("button");
        button = button.deleteIn([selectedIndex, "sub_button", selectedSubIndex]);
        this.dispatch(types.UPDATE_MENU, button);
        this.dispatch(types.SELECT_SUB_ITEM, button.getIn([selectedIndex, "sub_button"]).size > 0 ? 0 : -1);
    };

    getMPNewsDetail = async (media_id) => {
        return await WebApi.getMPNewsDetail(media_id);
    };

    getImageDetail = async (media_id) => {
        return await WebApi.getImageDetail(media_id);
    };
    getNewsDetail = async (id) => {
        return await WebApi.getNewsDetail(id);
    }


}